/*Author:S.Genikhov
Creatin Date:
Last modified:11/Sept/2010
Reviewed by :

Version 3 with possiblitiest to : Login,get,Bye and simple Security check.
Support only TCP connection.

Short project description:
Bases reallisation of FTP server command like : cd ,send recieve file and commands,
by using of state machine*/


#ifndef __INTERNALFTPCLIENT__
#define __INTERNALFTPCLIENT__

#include    "generclient.h"
#include    "ftpprotocol.h"


#define             TCP_PORT             (9734)/*Define the connection port of FTPclient to FTP server*/
#define             IP                   "127.0.0.1"
#define             OUT
#define             INOUT

/*Reads data from _packet and fill _header*/
void FTPReadHeadr(void* _buff,OUT FTPHead* _header);

/*Gets the user promt from STDIN and activats funcigton FTPCommandParser* Returns value is
return of  FTPCommandParser, command has to from events ( "ftpprotocol.h") */
static int FTPGlientGetUserPromt(OUT char* command,OUT char* param);

/*Simple devides user promt in two parts separated with 1st " "(space) symbol,
Returns 0 if success, and 1 of failes (if promt more than MAXPROMTLENGH  doesn't devide the promt and returns 1 )*/
static int FTPCommandParser(void* _buffer,OUT char* _command,OUT char* _param);


/*Returns index of found species in a list of string,
if object not found returns -1*/
static int FTPFinedInListOfStr(char* _str, char* _liststr,int _sizeoflist, int _lenghtstr);

/*Fill header of mesassage , all exept of datasize of package*/
static void FTPFillHeadr(void* _headbuf,size_t _positon,uint _islast,uint _error, uint _iscommand);
/*Convert contant of Header from network to host ntohl*/
static void FTPclientConvertHeader(FTPHead* recievbuf);

/*Add command to buffer.
Starts with header and update the datasize field in header.
Returns total lenght of package (data+header)
*/
static int FTPClientAddHeader(OUT char* _headerbuf,char* _command,char* _param);

/*Remove header from accepted packet.
If packet relates to 'Command' -- Simple devides accepted string in two parts devided by " "(space),
1st part puts into _command and second parts puts into _param.
Returns 0 if success and 1 if message more than MAXPROMTLENGH ,in this case doesn't devide
the promt and returns 1 */
static int  FTPClientGetCommand(char* _recievbuf,INOUT char* _command, INOUT char* _param);

/*
If packet relates to File transfre data to apporopriate file. WHile  all data not recieved.*/
static int FTPClientGetFile(int _socketID,void* _recievbuf);

/*Open new file, returns the file descriptor*/
static FILE* FTPClientOpenNewFile(void);

/*Gets the buffer and check is it file or command string, returns:
 1 if File,
 0 if String,
-1 if other value, (at the this point its will be treated as error*/
static int FTPClientIsFile(void* _recievbuf);

/*Reads data from _packet and fill _header*/
void FTPReadHeadr(void* _buff,OUT FTPHead* _header);


#endif /* __INTERNALFTPCLIENT__*/
